﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace blenderdriver
{
    internal class shell
    {
        public static string ExrPath => "d:\\temp\\out.exr";
        public static string InMeshPath => "d:\\temp\\inmesh";
        public static string OutMeshPath => "d:\\temp\\outmesh";
        public static async Task CleanOutput()
        {
            System.IO.Directory.Delete(OutMeshPath, true);
            System.IO.Directory.CreateDirectory(OutMeshPath);
            System.IO.File.Delete(ExrPath);
        }
        public static async Task CopyRes()
        {
            try
            {
                var files = System.IO.Directory.GetFiles("res", "*.*", SearchOption.AllDirectories);
                var path = System.IO.Path.GetFullPath("res");
                foreach (var f in files)
                {
                    var fullname = System.IO.Path.GetFullPath(f);
                    var onlyname = fullname.Substring(path.Length + 1);
                    var targetpath = System.IO.Path.GetDirectoryName(fullname);
                    if (System.IO.Directory.Exists(targetpath) == false)
                        System.IO.Directory.CreateDirectory(targetpath);

                    System.IO.File.Copy(fullname, "d:\\temp\\" + onlyname,true);
                }
            }
            catch (Exception err)
            {
                Console.Error.WriteLine(err.ToString());
                return;
            }
        }
        public static async Task CallBlendSilent(string blendfullpath, string pythonfile)
        {
            System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
            info.FileName = blendfullpath;
            info.WorkingDirectory = System.IO.Path.GetDirectoryName(blendfullpath);
            info.Arguments = "-b -P " + pythonfile;
            info.UseShellExecute = false;
            info.RedirectStandardOutput = true;
            info.RedirectStandardError = true;
            info.CreateNoWindow = true;

            System.Diagnostics.Process proc = null;
            try
            {
                proc = new System.Diagnostics.Process();
                proc.OutputDataReceived += (s, t) =>
                {
                    Console.WriteLine("Blender=>" + t.Data);
                };
                proc.ErrorDataReceived += (s, t) =>
                {
                    Console.Error.WriteLine("Blender=>" + t.Data);
                };
                proc.StartInfo = info;

                proc.Start();
                proc.BeginErrorReadLine();
                proc.BeginOutputReadLine();
            }
            catch (Exception err)
            {
                Console.Error.WriteLine(err.ToString());
                return;
            }

            await proc.WaitForExitAsync();

        }
    }
}
